package idiet.wizards.page;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.bean.CustomerPal;
import idiet.bean.NurseTarget;
import idiet.service.CustomerPalService;
import idiet.service.NurseTargetService;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class WizardPageCustomerTraceUpdate extends WizardPage {
	private CustomerPalService palService=(CustomerPalService)Activator.getApplicationContext().getBean(CustomerPalService.class);
	private NurseTargetService nurseTargetService=(NurseTargetService)Activator.getApplicationContext().getBean("nurseTargetService");
	private Text textName;
	private Text textAge;
	private Text textHeight;
	private Text textWeight;
	private Text textBust;
	private Text textWaist;
	private Text textHip;
	private Text textComment;
	private DateTime dateTimeStart;
	private DateTime dateTimeEnd;
	private Combo comboPal;
	private Combo comboTarget;
	//private String name;
	private Customer customer;
	/**
	 * Create the wizard.
	 */
	public WizardPageCustomerTraceUpdate() {
		super("wizardPage");
		setTitle("Wizard Page title");
		setDescription("Wizard Page description");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(3, false));
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Name");
		
		textName = new Text(container, SWT.BORDER);
		textName.setEditable(false);
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("Age");
		
		textAge = new Text(container, SWT.BORDER);
		textAge.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textAge.getText().trim().equals("")){
					setErrorMessage("age must not be empty ! ");
					setPageComplete(false);
				}else{
					if(!StringUtils.isNumeric(textAge.getText())){
						textAge.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textAge.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
				}
			}
		});
		textAge.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_2 = new Label(container, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("Height");
		
		textHeight = new Text(container, SWT.BORDER);
		textHeight.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textHeight.getText().trim().equals("")){
					setErrorMessage("age must not be empty ! ");
					setPageComplete(false);
				}else{
					if(!StringUtils.isNumeric(textHeight.getText())){
						textHeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textHeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
				}
			}
		});
		textHeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblKg = new Label(container, SWT.NONE);
		lblKg.setText("(cm)");
		
		Label lblNewLabel_3 = new Label(container, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setText("Weight");
		
		textWeight = new Text(container, SWT.BORDER);
		textWeight.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textWeight.getText().trim().equals("")){
					setErrorMessage("age must not be empty ! ");
					setPageComplete(false);
				}else{
					if(!StringUtils.isNumeric(textWeight.getText())){
						textWeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textWeight.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
				}
			}
		});
		textWeight.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblKg_1 = new Label(container, SWT.NONE);
		lblKg_1.setText("(Kg)");
		
		Label lblNewLabel_6 = new Label(container, SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_6.setText("Bust");
		
		textBust = new Text(container, SWT.BORDER);
		textBust.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
					if(!StringUtils.isNumeric(textBust.getText())){
						textBust.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textBust.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
			}
		});
		textBust.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblCm = new Label(container, SWT.NONE);
		lblCm.setText("(cm)");
		
		Label lblNewLabel_7 = new Label(container, SWT.NONE);
		lblNewLabel_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_7.setText("Waist");
		
		textWaist = new Text(container, SWT.BORDER);
		textWaist.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
					if(!StringUtils.isNumeric(textWaist.getText())){
						textWaist.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textWaist.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
			}
		});
		textWaist.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label = new Label(container, SWT.NONE);
		label.setText("(cm)");
		
		Label lblNewLabel_8 = new Label(container, SWT.NONE);
		lblNewLabel_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_8.setText("Hip");
		
		textHip = new Text(container, SWT.BORDER);
		textHip.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
					if(!StringUtils.isNumeric(textHip.getText())){
						textHip.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						setErrorMessage("age must be numeric ! ");
						setPageComplete(false);
					}else{
						textHip.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
						setErrorMessage(null);
						setPageComplete(true);
					}
			}
		});
		textHip.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(container, SWT.NONE);
		label_1.setText("(cm)");
		
		Label lblNewLabel_4 = new Label(container, SWT.NONE);
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_4.setText("Customer Pal");
		
		comboPal = new Combo(container, SWT.NONE);
		comboPal.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<CustomerPal> pals=palService.getCustomerPalsByCustomerPalName(null);
		for(CustomerPal pal:pals){
			comboPal.add(pal.getName());
			comboPal.setData(pal.getName(), pal.getId());
		}
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_5 = new Label(container, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_5.setText("Nurse Target");
		
		comboTarget = new Combo(container, SWT.NONE);
		comboTarget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<NurseTarget> targets=this.nurseTargetService.getNurseTargetsByNurseTargetName(null);
		for(NurseTarget target:targets){
			comboTarget.add(target.getName());
			comboTarget.setData(target.getName(), target.getId());
		}
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_9 = new Label(container, SWT.NONE);
		lblNewLabel_9.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
		lblNewLabel_9.setText("Forecast Start Date");
		
		dateTimeStart = new DateTime(container, SWT.BORDER);
		GridData gd_dateTimeStart = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_dateTimeStart.widthHint = 244;
		dateTimeStart.setLayoutData(gd_dateTimeStart);
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_10 = new Label(container, SWT.NONE);
		lblNewLabel_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_10.setText("Forecast End Date");
		
		dateTimeEnd = new DateTime(container, SWT.BORDER);
		GridData gd_dateTimeEnd = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_dateTimeEnd.widthHint = 244;
		dateTimeEnd.setLayoutData(gd_dateTimeEnd);
		Calendar calendar=GregorianCalendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 30);
		dateTimeEnd.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		new Label(container, SWT.NONE);
		
		Label lblNewLabel_11 = new Label(container, SWT.NONE);
		lblNewLabel_11.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblNewLabel_11.setText("Comment");
		
		textComment = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textComment.heightHint = 69;
		textComment.setLayoutData(gd_textComment);
		new Label(container, SWT.NONE);
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
		if(customer!=null){
			this.textName.setText(customer.getName());
			this.textAge.setText(new Integer(customer.getAge()).toString());
			this.textHeight.setText(customer.getHeight().toString());
			this.textWeight.setText(customer.getWeight().toString());
		}
	}
	
	@Override
	protected IDialogSettings getDialogSettings() {
		// TODO Auto-generated method stub
		DialogSettings settings=new DialogSettings("settings");
		settings.put("customerid", customer.getId());
		settings.put("customername", customer.getName());
		settings.put("height", textHeight.getText());
		settings.put("waist", textWaist.getText());
		settings.put("hip", textHip.getText());
		settings.put("bust", textBust.getText());
		if(!comboPal.getText().trim().equals("")){
			settings.put("pal", comboPal.getData(comboPal.getText()).toString());
		}else{
			settings.put("pal", "");
		}
		if(!comboTarget.getText().trim().equals("")){
			settings.put("target", comboTarget.getData(comboTarget.getText()).toString());
		}else{
			settings.put("target", "");
		}
		settings.put("starttime", dateTimeStart.toString());
		settings.put("endtime", dateTimeEnd.toString());
		settings.put("commen", textComment.getText());
		return settings;
	}
	
}
